package com.qf.web.controller;

import com.qf.domain.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrederService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartserviceImpl;
import com.qf.service.impl.OrederServiceImpl;
import com.qf.util.RandomUtil;
import com.qf.util.StringUtil;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.ietf.jgss.Oid;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet {
    //地址
    public String getOrderView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/redirct:/login.jsp";
        }
        //获取购物车的商品信息
        try {
            CartService cartService = new CartserviceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            //获取收获地址
            AddressService addressService = new AddressServiceImpl();
            List<Address> addList = addressService.find(user.getId());
            //放入与对象
            request.setAttribute("cartList", cartList);
            request.setAttribute("addList", addList);
            //转发
            return "/order.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "结账失败");
            return "/message.jsp";
        }

    }

    //添加订单
    public String addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/redirct:/login.jsp";
        }
        //获取地址
        String aid = request.getParameter("aid");
        if (StringUtil.isEmpty(aid)) {
            request.setAttribute("msg", "地址不能为空");
            return "/message.jsp";
        }
        //创建业务对象
        OrederService orederService = new OrederServiceImpl();
        //获取购物车信息 获取money
        CartService cartService = new CartserviceImpl();
        List<Cart> cartList = cartService.find(user.getId());
        if (cartList == null || cartList.size() == 0) {
            request.setAttribute("msg", "购物车不能为空");
            return "/message.jsp";
        }
        //取钱
        BigDecimal money = new BigDecimal(0);
        for (Cart cart : cartList) {
            money = money.add(cart.getMoney());
        }
        //创建订单
        try {
            String oid = RandomUtil.createOrderId();
            Order order = new Order(oid, user.getId(), money, "1", new Date(), Integer.parseInt(aid));
            orederService.submitOrder(order, cartList);
            request.setAttribute("order", order);
            return "/orderSuccess.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "添加订单失败");
            return "/message.jsp";
        }
    }

    //查看订单
    public String getOrderList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/redirct:/login.jsp";
        }
        //创建业务对象
        try {
            OrederService orederService = new OrederServiceImpl();
            List<Order> orderList = orederService.find(user.getId());
            //放入域对象
            request.setAttribute("orderList", orderList);
            return "/orderList.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询订单失败");
            return "/message.jsp";
        }
    }

    //查看订单详情
    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return"redirect:/login.jsp";
        }
        //获取订单号
        String oid = request.getParameter("oid");
        if (StringUtil.isEmpty(oid)) {
            request.setAttribute("msg", "订单不能为空");
            return "/message.jsp";
        }
        //创建业务对象
        try {
            OrederService orederService = new OrederServiceImpl();
            //查看订单
            Order order = orederService.findByOid(oid);
            //查看订单详情
            List<OrderDetail>orderDetails = orederService.findDetail(oid);
            //存值
            request.setAttribute("order",order);
            request.setAttribute("orderDetails",orderDetails);
            return "orderDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","查询订单详情失败");
            return "/message.jsp";
        }
    }

}

